package Leo.ribbon;

import com.netflix.loadbalancer.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class LoadBalanceDemo {

    public static void main(String[] args) {
//        Scanner scanner = new Scanner(System.in);
//        int port1 = scanner.nextInt();
//        int port2 = scanner.nextInt();
        int port1=11111,port2 =22222;
        BaseLoadBalancer loadBalancer = new BaseLoadBalancer();
        //可以自定义负载均衡规则，也可以使用默认规则
        loadBalancer.setRule(new LoadBalanceRuleImpl(loadBalancer));
        List<Server> serverList = new ArrayList();
        serverList.add(new Server("localhost",port1));
        serverList.add(new Server("localhost",port2));
        loadBalancer.setServersList(serverList);
        //ping机制，用于定时ping服务器，默认是DommyPing
        IPing ping = loadBalancer.getPing();
        System.out.println("Default ping class is DummyPing? :" + (ping instanceof DummyPing));
        loadBalancer.setPing(new PingUrl());
        //每隔多少秒ping一次
        loadBalancer.setPingInterval(2);
        for(int i = 7;i> 0;i--) {
            Server server = loadBalancer.chooseServer(null);
            System.out.println(server.getHostPort());
        }
    }
}
